import { Dialog, Notify } from 'vant'
import router from '../router'
import GCRequest from '../utils/request'
import { BASE_URL, TIMEOUT } from '../utils/request/config'

const gcRequest = new GCRequest({
  baseURL: BASE_URL,
  timeout: TIMEOUT,
  interceptors: {
    requestInterceptors: (config) => {
      return config
    },
    requestInterceptorsCatch: (err) => {
      console.log(err)
    },
    responseInterceptors: (res) => {
      return res
    },
    responseInterceptorsCatch: (err) => {
      if (err.response.status === 401) {
        Dialog.confirm({
          message: '该功能需要登录,确认登录吗?',
          confirmButtonColor: '#228efc'
        }).then(() => {
          router.replace('/login')
        })
        localStorage.clear()
      } else {
        Notify({
          message: err.response.data.message,
          type: 'danger'
        })
      }
      return err
    }
  }
})

export default gcRequest
